/**
 * RadialInterpolatorTest.java
 * colourmap
 * Copyright (C) 2010 Alan W.F. Boyd
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact the author via the google code web page for the project
 * <http://colourmap.googlecode.com>
 */
package com.googlecode.colourmap.unittests;

import java.util.HashMap;

import org.junit.Before;
import org.junit.Test;

import com.googlecode.colourmap.Point;
import com.googlecode.colourmap.interpolator.RadialInterpolator;

/**
 * This class takes care of unit testing the radial interpolator
 * @author Alan W.F. Boyd
 *
 */
public class RadialInterpolatorTest
{
	private RadialInterpolator ri;
	
	/**
	 * Before the tests are run, the RadialInterpolator object is created
	 */
	@Before 
	public void beforeTests()
	{
		ri = new RadialInterpolator();
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * If there is only one point in the data set, then every coordinate should have the same value.
	 */
	@Test
	public void testSinglePoint()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(-2, -2), 5f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(0, 0)) == 5f);
		assert(ri.getValue(new Point(-7, -7)) == 5f);
		assert(ri.getValue(new Point(-7, 7)) == 5f);
		assert(ri.getValue(new Point(7, -7)) == 5f);
		assert(ri.getValue(new Point(7, 7)) == 5f);
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * If getValue() is called for a point that already exists, return that point
	 */
	@Test
	public void testGettingKnownPoint()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(-10, -10), 22f);
		map.put(new Point(-7, 8), 23f);
		map.put(new Point(9, 6), -11f);
		map.put(new Point(2, 3), -111f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(-10, -10)) == 22f);
		assert(ri.getValue(new Point(-7, 8)) == 23f);
		assert(ri.getValue(new Point(9, 6)) == -11f);
		assert(ri.getValue(new Point(2, 3)) == -111f);
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * If two points overlap and do not interfere, then all coordinates should have the same value as the points
	 */
	@Test
	public void testTwoPointsOverlappingNonInterfering()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(-2, -2), 5f);
		map.put(new Point(-2, -2), 5f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(0, 0)) == 5f);
		assert(ri.getValue(new Point(-7, -7)) == 5f);
		assert(ri.getValue(new Point(-7, 7)) == 5f);
		assert(ri.getValue(new Point(7, -7)) == 5f);
		assert(ri.getValue(new Point(7, 7)) == 5f);
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * If two points overlap and exactly cancel out, then all other points should have a value of 0 (except possibly that same point)
	 */
	@Test
	public void testTwoPointsOverlappingAndInterfering()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(0, 0), 15f);
		map.put(new Point(0, 0), -15f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(-7, -7)) == 0f);
		assert(ri.getValue(new Point(-7, 7)) == 0f);
		assert(ri.getValue(new Point(7, -7)) == 0f);
		assert(ri.getValue(new Point(7, 7)) == 0f);
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * If two points do not interfere, then all coordinates should have the same value as the points
	 */
	@Test
	public void testTwoPointsNonInterfering()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(-2, -2), 5f);
		map.put(new Point(-2, 2), 5f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(0, 0)) == 5f);
		assert(ri.getValue(new Point(-7, -7)) == 5f);
		assert(ri.getValue(new Point(-7, 7)) == 5f);
		assert(ri.getValue(new Point(7, -7)) == 5f);
		assert(ri.getValue(new Point(7, 7)) == 5f);
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * Demonstrate that the effect of multiple nearby points is correctly calculated
	 */
	@Test
	public void testTwoPoints()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(-2, -2), 5f);
		map.put(new Point(-2, 2), 10f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(0, 0)) == 7.5f);
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * Ensures that a point surrounded by four equally distant, oppositely valued points will exactly cancel out.
	 */
	@Test
	public void testEquidistantPointsCancel()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(-2, -2), -5f);
		map.put(new Point(-2, 2), -5f);
		map.put(new Point(2, -2), 5f);
		map.put(new Point(2, 2), 5f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(0, 0)) == 0f);
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * Only consider the nearest four points in calculating a value
	 */
	@Test
	public void testMaxFourPoints()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(-2, -2), 5f);
		map.put(new Point(-2, 2), 5f);
		map.put(new Point(2, -2), 5f);
		map.put(new Point(2, 2), 5f);
		map.put(new Point(3, 3), 100000f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(0, 0)) == 5f);
	}
	
	/**
	 * Test method for {@link com.googlecode.colourmap.interpolator.RadialInterpolator#getValue(com.googlecode.colourmap.Point)}.
	 * Only consider the nearest four points in calculating a value
	 */
	@Test
	public void testFourPointCalculation()
	{
		HashMap<Point, Float> map = new HashMap<Point, Float>();
		map.put(new Point(-2, -2), 10f);
		map.put(new Point(-2, 2), 20f);
		map.put(new Point(2, -2), 15f);
		map.put(new Point(2, 2), 7f);
		
		ri.setPoints(map);
		
		assert(ri.getValue(new Point(0, 0)) == 13f);
	}

}
